#include"list.h"


//------------------------------------------
//function about listdata     --------------
//------------------------------------------


void Init(ListData* data)
{
    scanf("%d%f",&data->a,&data->b);
}

int Judge(ListData* data,ListData* comData)
{
   if(data->a==comData->a&&data->b==comData->b)
   { 
        return 1;
   }
   else {
   return 0;
   }
}

void DataCopy(ListData* des,ListData* res)
{
   des->a=res->a;
   des->b=res->b;
}


void show(ListData* data)
{
    printf("%d %f\n",data->a,data->b);
}

//------------------------------------------
//function about sequencelist --------------
//------------------------------------------

SequenceList* InitList(int size)
{
    SequenceList* list=(SequenceList*)malloc(sizeof(SequenceList));
    list->data=(ListData*)malloc(size*sizeof(ListData));
    list->memSize=size;
    list->dataSize=size;
    for(int i=0;i<size;i++)
    {
        printf("init the %d data:\n",i);
        Init(&(list->data[i]));
    };
    return list;
}

int DestroyList(SequenceList* beDestroied)
{
    free(beDestroied->data);
    free(beDestroied);
    return 1;
}


void ClearList(SequenceList* beCleared)
{
    beCleared->dataSize=0;
} 



int IfEmpty(SequenceList* beJudged)
{
    if(beJudged->dataSize==0)
    {
        return 0;
    }
    else {
    return 1;
    }

}



int ListLength(SequenceList* list)
{
    return list->dataSize;
}



ListData* Getelement(SequenceList* list,int location)
{
    return &list->data[location-1];
}



int LocateElement(SequenceList* list,ListData* data)
{
   for(int i=0;i<list->dataSize;i++)
   {
    if(Judge(&list->data[i], data))
    {
        return i;
    }
   }
   return -1; //equal data don`t exist
}



int Insert(SequenceList* list,int location,ListData* data)//everytime you call it,the size of data will be larger. To be hones,it`s a wast of time
{
   if (list->dataSize==list->memSize) 
{
   list->memSize++;
   list->dataSize++;
   ListData* tmp=(ListData*)malloc(list->memSize*sizeof(ListData)); //make a larger array in heap 
   for(int i=0;i<list->memSize-1;i++)         //put data in it
   {
   tmp[i]=list->data[i];
   } 
   free(list->data);                   //destroy previous array
   list->data=tmp;                      //replace by new on
   memcpy(list->data+location,list->data-1+location,(list->dataSize-location+1)*sizeof(ListData));
   DataCopy(&list->data[location-1], data);   //insert new data in location
   return 1;                            //ok~
}
   else 
{
   memcpy(list->data+location,list->data-1+location,(list->dataSize-location+1)*sizeof(ListData));
   DataCopy(&list->data[location-1], data);   //insert new data in location
   return 1;                            //ok~ 
}                           
}

int Delet(SequenceList* list,int location)
{
    memcpy(list->data-1+location,list->data+location,(list->dataSize-location+1)*sizeof(ListData));
    list->dataSize--;
    return 0;
}


void DataShow(SequenceList* list)
{
    for(int i=0;i<list->dataSize;i++)
    {   
        printf("the %d data is:\n",i);
        show(&list->data[i]);
    }
}



